/*
 * pps是玺得科技公司针对pi-top产品生产质量检测而研发的系统，仅限于本公司使用与研究.
 */
package com.pitop.serial.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.serial.entity.SerialNumber;
import com.pitop.serial.entity.SerialNumberType;
import com.pitop.serial.service.SerialNumberSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 获取芯片 mcu的序列号 （仅用于对OS的接口提供）
 *
 * @author leo
 */
@Slf4j
@Controller
@RequestMapping("/sn")
@Api(value = "This controller is only used to receive the serial number of MCU.", description = "This controller is only used to receive the serial number of MCU.")
public class CollectSerialNumberCtrl {


    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private SerialNumberSVImpl serialNumberSVImpl;

    /**
     * 获取产品sn码 （仅用于OS自动提交序列号）
     *
     * @return BeanRet
     */
    @ApiOperation(value = "collect serial number", notes = "collect serial number")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "JSON format", paramType = "body")
    })
    @PostMapping("/collect")
    @ResponseBody
    public BeanRet build(@RequestBody String json) {
        log.info("收集到机器的sn码 <=== " + json);
        Map<String, String> snMap = JSON.parseObject(json, Map.class);
        String master_mcu = "master_mcu";//主板
        if (snMap.containsKey(master_mcu)) {
            SerialNumber serialNumber = new SerialNumber();
            serialNumber.setSn(snMap.get("master_mcu"));
            serialNumber.setSnCode(SerialNumberType.Master.name());
            serialNumber.setSnName(SerialNumberType.Master.type);
            serialNumberSVImpl.save(serialNumber);
        }

        String battery_mcu = "battery_mcu";//电池
        if (snMap.containsKey(battery_mcu)) {
            SerialNumber batterySerialNumber = new SerialNumber();
            batterySerialNumber.setSn(snMap.get(battery_mcu));
            batterySerialNumber.setSnCode(SerialNumberType.Battery.name());
            batterySerialNumber.setSnName(SerialNumberType.Battery.type);
            serialNumberSVImpl.save(batterySerialNumber);
        }

        String screen_mcu = "screen_mcu";//屏幕
        if (snMap.containsKey(screen_mcu)) {
            SerialNumber screenSerialNumber = new SerialNumber();
            screenSerialNumber.setSn(snMap.get(screen_mcu));
            screenSerialNumber.setSnCode(SerialNumberType.Screen.name());
            screenSerialNumber.setSnName(SerialNumberType.Screen.type);
            serialNumberSVImpl.save(screenSerialNumber);
        }

        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, (Object) json);
    }

}
